﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbLinq.Data.Linq;
using System.Data;
using System.IO;

namespace BusStopDataAccess
{
    /// <summary>
    /// A disposable wrapper for the database object that will disconnect the database connection and any other managed resources when the object is disposed
    /// </summary>
    public class DisposeableScheduleInfo : IDisposable, IScheduleInfo
    {
        /// <summary>
        /// Constructs a wrapper around the specified db connection and database object
        /// </summary>
        /// <param name="db"></param>
        /// <param name="si"></param>
        public DisposeableScheduleInfo(IDbConnection db, ScheduleInfo si)
        {
            _db = db;
            _si = si;
        }

        /// <summary>
        /// Textwriter to use for debug logging
        /// </summary>
        public TextWriter Log
        {
            get { return _si.Log; }
            set { _si.Log = value; }
        }

        private IDbConnection _db;
        private ScheduleInfo _si;

        /// <summary>
        /// Disposes the database object and its associated db connections
        /// </summary>
        public void Dispose()
        {
            try { _si.Dispose(); }
            finally
            {
                _db.Dispose();
            }
        }

        /// <summary>
        /// The list of all Bus stops
        /// </summary>
        public Table<BusStops> BusStops
        { get { return _si.BusStops; } }

        /// <summary>
        /// The list of all departures for all busses and all stops (Very Large Avoid unneccesary use!)
        /// </summary>
        public Table<Departures> Departures
        { get { return _si.Departures; } }

        /// <summary>
        /// The list of all routes
        /// </summary>
        public Table<Routes> Routes
        { get { return _si.Routes; } }

        /// <summary>
        /// The list of all service schedules
        /// </summary>
        public Table<Service> Service
        { get { return _si.Service; } }

        /// <summary>
        /// The list of dates associated with a service
        /// </summary>
        public Table<ServiceDates> ServiceDates
        { get { return _si.ServiceDates; } }

        /// <summary>
        /// A mapping showing the distinct routes that depart from a given stop
        /// </summary>
        public Table<RouteStops> RouteStops
        { get { return _si.RouteStops; } }

        /// <summary>
        /// Commits any changes to the data 
        /// </summary>
        public void SubmitChanges()
        { _si.SubmitChanges(); }
    }
}
